<?php

/*
	TF2 Gather Bot v0.11.0
	Módulo para PHP-IRC v2.2.1 (www.phpbots.org)
	Creado por Foxtreme (danig.fox@gmail.com)
	para #gather.es @ Quakenet.org
	
	Archivo: ./modules/gather/topic.php
*/

// Clase que se encarga de comprobar y actualizar el topic del canal.

class Topic {
	
	var $core;
	var $topic;
	var $topic_bool = FALSE;
	
	function __construct($object){
		$this->core = $object;
	}
	
	// Bucle que actualiza el topic
	public function _check($arguments) {
		
		$newtopic = $this->_generate();
		
		// Comprueba si el topic generado es el actual
		$newtopic_trim = ltrim($newtopic, '');
		$topic_trim = $this->topic;
		if ($topic_trim != $newtopic_trim) {
			$this->_set ($newtopic, $this->core->config->chan);
			$this->topic = $newtopic;
		}
		return TRUE;
	}
	
	// Comando !status
	public function _command_status() {
		$newtopic = $this->_generate();
		$this->_set ($newtopic, $this->core->config->chan);
		$this->topic = $newtopic;
	}
	
	// Genera el topic 
	function _generate() {
		
		// Base del topic
		// (%normalplayers%+%medicplayers%) [ Gather%skill%: %players% | Médicos: %medics% ] - [ Mapas: %maps% ]
		$base = $this->core->options['topic'];
		
		// Esta variable alterna el topic cada vez que se genera para que funcione el comando !status
		if ($this->topic_bool) {
			$bool = '';
			$this->topic_bool = FALSE;
		}
		else {
			$bool = '';
			$this->topic_bool = TRUE;
		}
		
		// Generamos distintas partes del topic
		$normalplayers = 0;
		$medicplayers = 0;
		$skill = '(Mid+)';
		$players = 'x - x - x - x - x - x - x - x - x - x - x - x';
		$medics = 'x - x';
		$maps = '(ninguno)';
		
		// Sustituimos los valores
		$gen = $bool . $base;
		$gen = preg_replace("/%normalplayers%/", 5, $gen);
		$gen = preg_replace("/%medicplayers%/", 2, $gen);
		$gen = preg_replace("/\[ Gather/", '[ 4Gather', $gen);
		$gen = preg_replace("/%skill%/", $skill, $gen);
		$gen = preg_replace("/%players%/", '7' . $players . '', $gen);
		$gen = preg_replace("/Médicos/", '4Médicos:', $gen);
		$gen = preg_replace("/%medics%/", '7' . $medics . '', $gen);
		$gen = preg_replace("/Mapas/", 'Maps', $gen);
		$gen = preg_replace("/%maps%/", $maps, $gen);
		
		return $gen;
	}
	
	// Setea un topic
	public function _set($topic, $chan = FALSE) {
		if ($chan) {
			$this->core->ircClass->sendRaw('TOPIC ' . $chan . ' :' . $topic);
		}
		else {
			$this->core->ircClass->sendRaw('TOPIC ' . $this->core->config->chan . ' :' . $topic);
		}
	}
	
}

?>